package org.anupta.game.entity.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.anupta.game.entity.entities.Account;
import org.anupta.game.utils.others.Config;
import org.anupta.game.utils.sql.Database;

public class AccountDAO extends DAO<Account> {

	@Override
    public Account find(int id) {
            Connection connection = null;
            PreparedStatement statement = null;
            ResultSet resultSet = null;
            Account account = null;
            try {
                connection = Database.getDatabase(Config.LOGIN[0]).getSource().getConnection();
                statement = connection.prepareStatement("SELECT * FROM accounts WHERE guid = ?;");
                statement.setLong(1, id);
                resultSet = statement.executeQuery();

                if (resultSet.first()) {
                    account = new Account(
                    		resultSet.getInt("guid"),
                    		resultSet.getString("name"),
                    		resultSet.getString("pseudo"),
                    		resultSet.getString("answer"),
                    		resultSet.getByte("gmLevel"),
                    		resultSet.getString("players")
                    		);
                }
                
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException ex) {
                Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    resultSet.close();
                    statement.close();
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return account;
    }

    @Override
    public Account find(String name) {
            Connection connection = null;
            PreparedStatement statement = null;
            ResultSet resultSet = null;
            Account account = null;
            try {
                connection = Database.getDatabase(Config.LOGIN[0]).getSource().getConnection();
                statement = connection.prepareStatement("SELECT * FROM accounts WHERE name = ?;");
                statement.setString(1, name);
                resultSet = statement.executeQuery();

                if (resultSet.first()) {
                    account = new Account(
                    		resultSet.getInt("guid"),
                    		resultSet.getString("name"),
                    		resultSet.getString("pseudo"),
                    		resultSet.getString("answer"),
                    		resultSet.getByte("gmLevel"),
                    		resultSet.getString("players")
                    		);
                }
                
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException ex) {
                Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    resultSet.close();
                    statement.close();
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return account;
    }

    @Override
    public List<Account> findAll() {
		return null;
    }

    @Override
    public void commit() {
    	for (Account account : getCreate().values()) {
    		Connection connection = Database.getDatabase(Config.LOGIN[0]).getConnection();
    		PreparedStatement statement = null;
    		try {
				statement = connection.prepareStatement("INSERT INTO accounts" +
														" VALUES(?,?,?,?,?,?);");
				statement.setInt(1, account.getGuid());
				statement.setString(2, account.getName());
				statement.setString(3, account.getPseudo());
				statement.setString(4, account.getAnswer());
				statement.setInt(4, account.getGmLevel());
				statement.setString(5, account.getPlayersString());
				statement.executeUpdate();
				
				statement.close();
				connection.close();
				getCreate().remove(account);
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					statement.close();
					connection.close();
					getCreate().remove(account);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
        }

        for (Account account : getUpdate().values()) {
        	Connection connection = Database.getDatabase(Config.LOGIN[0]).getConnection();
    		PreparedStatement statement = null;
    		try {
				statement = connection.prepareStatement("UPDATE accounts SET " +
														"name = ?," +
														"pseudo = ?," +
														"answer = ?," +
														"gmLevel = ?," +
														"players = ?" +
														" WHERE guid = ?;");
				statement.setString(1, account.getName());
				statement.setString(2, account.getPseudo());
				statement.setString(3, account.getAnswer());
				statement.setInt(4, account.getGmLevel());
				statement.setString(5, account.getPlayersString());
				statement.setInt(6, account.getGuid());
				statement.executeUpdate();
				
				statement.close();
				connection.close();
				getUpdate().remove(account);
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					statement.close();
					connection.close();
					getUpdate().remove(account);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
        }

        for (Account account : getDelete().values()) {
        	Connection connection = Database.getDatabase(Config.LOGIN[0]).getConnection();
    		PreparedStatement statement = null;
    		try {
				statement = connection.prepareStatement("DELETE FROM accounts WHERE guid = ?;");
				statement.setInt(1, account.getGuid());
				statement.executeUpdate();
				
				statement.close();
				connection.close();
				getDelete().remove(account);
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					statement.close();
					connection.close();
					getDelete().remove(account);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
        }
    }

}
